//
//  LYMineIntrestViewController.m
//  artist
//
//  Created by Jay_Apple on 16/5/9.
//  Copyright © 2016年 viviyoo. All rights reserved.
//

#import "LYMineIntrestViewController.h"
#import "LYMineIntrestBtn.h"
#import "LYChooseListView.h"
#import "LYMySubjectListModel.h"
#import "LYSubjectListDBTool.h"
@interface LYMineIntrestViewController ()


@property (weak, nonatomic) LYChooseListView *listView;

//@property (weak, nonatomic) IBOutlet UIView *bottomView;
//@property (weak, nonatomic) IBOutlet UIView *contentView;
@end

@implementation LYMineIntrestViewController


- (instancetype)init{
    self = [super init];
    self.selectedMaxCount = 3;
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setUpNav];
    [self getInterestsData];
    
}


- (void)viewDidLayoutSubviews{
    
    [super viewDidLayoutSubviews];
    
    WS(weakSelf);
    
    self.listView.block = ^(NSString *item){
        
        [weakSelf addInterests:item];
    };
    
}

#pragma mark - 初始化
- (void)setUpNav{
    
    
    if(!(self.navigationItem.title.length > 0)){
        self.navigationItem.title = @"我的兴趣";
    }
    
    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithTitle:@"保存" style:UIBarButtonItemStyleDone target:self action:@selector(saveIntrest)];
    self.navigationItem.rightBarButtonItem = rightItem;
    
}

#pragma mark - 网络请求
/**
 *  获取兴趣
 */
- (void)getInterestsData{
    
    [MBProgressHUD showMessage:nil];
    
    [LYRequestTool getMySubjectListWithToken:LYToken success:^(NSArray<LYMySubjectListModel *> *models) {
        [MBProgressHUD hideHUD];
//        NSMutableArray *interets = [NSMutableArray array];
        
        for (LYMySubjectListModel *model in models) {
            
            [self.interests addObject:model.title];
            
        }
        
//        self.interests = interets;
        [self updateInterest];
        
    } failure:^(NSString *code, NSString *message) {
        [MBProgressHUD hideHUD];
        [MBProgressHUD showError:message];
    }];
    
    
}


- (NSMutableArray *)interests{
    if (_interests == nil){
        _interests = [NSMutableArray array];
        
    }
    return _interests;
}
/**
 *  更新兴趣
 */
- (void)updateInterestDataWithSubjectStr:(NSString *)subjects{
    
    if (![self.navigationItem.title isEqualToString:@"我的兴趣"]){
        
        if (self.confirmSubjectids){
            self.confirmSubjectids(subjects);
        }
        [self.navigationController popViewControllerAnimated:YES];
        return;
    }
    
    [MBProgressHUD showMessage:nil];

    WS(weakSelf)
    [LYRequestTool updateMySubjectListWithToken:LYToken subject_id:subjects success:^{
        [MBProgressHUD hideHUD];
        [MBProgressHUD showSuccess:@"修改成功"];
        if (weakSelf.confirmSubjectids){
            weakSelf.confirmSubjectids(subjects);
        }
        [self.navigationController popViewControllerAnimated:YES];
        
    } failure:^(NSString *code, NSString *message) {
        [MBProgressHUD hideHUD];
        [MBProgressHUD showError:message];
    }];
}

#pragma mark - 懒加载

- (LYChooseListView *)listView{
    
    if (_listView == nil) {
        
        _listView  = [LYChooseListView showOnView:self.bottomView WithMode:listModeNormal];
        
    }
    
    return _listView;
    
}


#pragma mark - 按钮点击

- (void)itemBtnOnclick:(UIButton *)btn{
    
    [self removeInterestAtIndex:btn.tag];
    
}

- (void)saveIntrest{
    
    
    NSMutableArray *sujectIDs = [NSMutableArray array];
    
    for (NSString *sub in self.interests) {
        
        NSInteger subID = [LYSubjectListDBTool getIDWithSubTitle:sub];
        [sujectIDs addObject:[NSString stringWithFormat:@"z%d",subID]];
    }

    
    NSString *subjects = [sujectIDs componentsJoinedByString:@","];
    [self updateInterestDataWithSubjectStr:subjects];
}

- (void)setSelectedMaxCount:(NSInteger)selectedMaxCount{
    _selectedMaxCount = selectedMaxCount;
}

#pragma mark - 其他
- (void)addInterests:(NSString *)item{
    
    for (NSString *intrest in self.interests) {
        
        if ([intrest isEqualToString:item] ) {
            return;
        }
        
    }
    
    if (self.interests.count >= self.selectedMaxCount) {
        
//        NSLog(@"s")
        NSString *message =  [NSString stringWithFormat:@"最多只能选择%zd个兴趣",self.selectedMaxCount];
        [MBProgressHUD showOnlyText:message];
    }else{
        
        [self.interests addObject:item];
        [self updateInterest];
    }
    
    
}


- (void)removeInterestAtIndex:(NSUInteger)index{
    
    [self.interests removeObjectAtIndex:index];
    [self updateInterest];
    
}

- (void)updateInterest{
    
    for (UIView *item in self.contentView.subviews) {
        [item removeFromSuperview];
    }
    
    
    NSUInteger index = 0;
    UIButton *lastBtn = nil;
    CGFloat X;
    CGFloat centerY = self.contentView.My_Height * 0.5;
    CGFloat W;
    CGFloat H = 25;
    
    for (NSString *item in self.interests) {
        
        LYMineIntrestBtn *btn = [[LYMineIntrestBtn alloc] init];
        
        btn.backgroundColor = [UIColor orangeColor];
        btn.titleLabel.font = [UIFont systemFontOfSize:13];
        [btn setTitle:item forState:UIControlStateNormal];
        [btn setImage:[UIImage imageNamed:@"删除"] forState:UIControlStateNormal];
        [btn addTarget:self action:@selector(itemBtnOnclick:) forControlEvents:UIControlEventTouchUpInside];
        btn.tag = index;
        index++;
        [btn sizeToFit];
        
        //frame
        if (lastBtn) {
            X = CGRectGetMaxX(lastBtn.frame) + 10;
        }else{
            
            X = 10;
        }
        
        W = btn.My_Width + 25;
        btn.My_Height = H;
        btn.My_Width = W;
        btn.My_X = X;
        btn.My_CenterY = centerY;
        
        [self.contentView addSubview:btn];
        
        lastBtn = btn;
    }
    
}



@end
